<?php

// +----------------------------------------------------------------------
// | Banner api控制器
// +----------------------------------------------------------------------
// | @author fancsen
// +----------------------------------------------------------------------

/**
 * Banner api控制器
 * @author fancsen
 */


namespace app\api\controller\v1;

use app\api\validate\Count;
use app\api\validate\IDMustBePostiveInt;
use app\api\model\Product as ProductModel;
use app\api\model\Category as CategoryModel;
use app\common\lib\exception\ApiException;

class Product
{
    /**
     * 获取指定数目新品
     * @http get
     * @param number $count 数目
     * @return array
     */
    public function getRecent($count = 15)
    {
        (new Count())->goCheck();

        $products = ProductModel::getRecentProducts($count);

        if($products->isEmpty()){
            throw new ApiException('请求的指定的商品不存在',404,50000);
        }
        
        $products = $products->hidden(['summary']);
        
        return show(config('api.success_status'),'ok',$products);
        
    }

    /**
     * 获取指定类目下的商品
     * @http get
     * @param number $id 类目的id值
     * @return array
     */
    public function getAllInCategory($id=null)
    {
        (new IDMustBePostiveInt())->goCheck();

        $products = CategoryModel::getProductsByCategoryId($id);

        if(!$products){
            throw new ApiException('请求指定的类目不存在',404,50000);
        }
        return show(config('api.success_status'),'ok',$products);
    }

    /**
     * 获取指定的商品详情
     * @http get
     * @param number $id 类目的id值
     * @return array
     */
    public function getOne($id=null)
    {
        (new IDMustBePostiveInt())->goCheck();

        $product = ProductModel::getProductById($id);

        if(!$product){
            throw new ApiException('请求指定的商品不存在',404,60000);
        }
        return show(config('api.success_status'),'ok',$product);
    }
}